package com.xzqwjw.taskmanager.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.leaderinfo.model.InfoJob;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class JobDao implements com.leaderinfo.service.OperateDatabaseService {

	private SQLiteDatabase objDB;
	private String strDBFile = "";

	public JobDao(String strDBFile) {
		this.strDBFile = strDBFile;
	}

	@Override
	public Map<String, Object> getRecordByAID(int intAID) {
		InfoJob objJob = null;
		Map<String, Object> mapInfo = null;

		objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);

		String strSQL = "SELECT * FROM info_cadre_job WHERE aid=?";
		String[] aryArgs = { String.valueOf(intAID) };

		Cursor objCursor = objDB.rawQuery(strSQL, aryArgs);
		if (objCursor.moveToFirst()) {
			objJob = new InfoJob();
			getInfo(objCursor, objJob);
			mapInfo = new HashMap<String, Object>();
			mapInfo.put("JobInfo", objJob);
		}
		objCursor.close();
		objDB.close();
		return mapInfo;
	}

	@Override
	public Map<String, Object> getRecordByBasicID(int intBasicID) {
		return null;
	}

	public int[] getHoldpostState(int intBasicID) {
		int[] listHoldpostState = new int[12];
		int i = 0;
		for (i = 0; i < 12; i++) {
			listHoldpostState[i] = 0;
		}
		i = 0;
		String strHoldpostState = "";

		objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);

		String strSQL = "SELECT holdpost_state FROM info_cadre_job WHERE id_basic=?";
		String[] aryArgs = { String.valueOf(intBasicID) };

		Cursor objCursor = objDB.rawQuery(strSQL, aryArgs);
		if (objCursor.getCount() > 0) {
			while (objCursor.moveToNext()) {
				strHoldpostState = objCursor.getString(objCursor
						.getColumnIndex("holdpost_state"));
				if (strHoldpostState.equals("����".toString())) {
					listHoldpostState[i] = 1;
				} else {
					listHoldpostState[i] = 0;
				}
				i += 1;
			}
		}
		objCursor.close();
		objDB.close();
		return listHoldpostState;
	}

	@Override
	public List<Map<String, Object>> getRecordsByBasicID(int intBasicID) {
		List<Map<String, Object>> listInfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> mapInfo = null;
		InfoJob objJob = null;

		objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);

		String strSQL = "SELECT * FROM info_cadre_job WHERE id_basic=?";
		String[] aryArgs = { String.valueOf(intBasicID) };

		Cursor objCursor = objDB.rawQuery(strSQL, aryArgs);
		if (objCursor.getCount() > 0) {
			while (objCursor.moveToNext()) {
				mapInfo = new HashMap<String, Object>();
				objJob = new InfoJob();
				getInfo(objCursor, objJob);
				mapInfo.put("JobInfo", objJob);
				listInfo.add(mapInfo);
			}
		}
		objCursor.close();
		objDB.close();
		return listInfo;
	}

	@Override
	public List<Map<String, Object>> getAllRecords() {
		return null;
	}

	private void getInfo(Cursor objCursor, InfoJob objJob) {
		objJob.AID = objCursor.getInt(objCursor.getColumnIndex("aid"));
		objJob.BasicID = objCursor.getInt(objCursor.getColumnIndex("id_basic"));
		objJob.HoldpostDate = objCursor.getString(objCursor
				.getColumnIndex("holdpost_date"));
		objJob.JobType = objCursor.getString(objCursor
				.getColumnIndex("job_type"));
		objJob.IsTeamMember = objCursor.getString(objCursor
				.getColumnIndex("is_team_member"));
		objJob.HoldpostState = objCursor.getString(objCursor
				.getColumnIndex("holdpost_state"));
		objJob.DepositionType = objCursor.getString(objCursor
				.getColumnIndex("deposition_type"));
		objJob.DepositionReferenceNumber = objCursor.getString(objCursor
				.getColumnIndex("deposition_reference_number"));
		objJob.DepositionDate = objCursor.getString(objCursor
				.getColumnIndex("deposition_date"));
		objJob.IsCommunicating = objCursor.getString(objCursor
				.getColumnIndex("is_communicating"));
		objJob.CommunicatingReason = objCursor.getString(objCursor
				.getColumnIndex("communicating_reason"));
		objJob.CommunicatingWay = objCursor.getString(objCursor
				.getColumnIndex("communicating_way"));
		objJob.ExchangeTo = objCursor.getString(objCursor
				.getColumnIndex("exchange_to"));
	}

	@Override
	public List<Map<String, Object>> getRecordsByWhere(String strWhere) {
		return null;
	}
}
